package com.otod.base.service;

import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.otod.commons.auth.SessionUserHolder;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.constants.LogConstant;
import com.otod.commons.utils.BeanUtils;
import com.otod.commons.utils.DateUtils;
import com.otod.commons.utils.SpringContextHolder;
import com.otod.sys.operater.log.service.IOperaterLogService;
import com.otod.sys.vo.AsmgOperaterLogVo;

/**
 * 基础service
 * @author jqiu
 *
 */
public abstract class BaseService{
	private static Logger logger = LoggerFactory.getLogger(BaseService.class);
	
	
	/**
	 * 获取唯一随机数
	 * @return
	 */
	protected String getUUID(){
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	/**
	 * 添加日志
	 * @param busiFunc 		业务功能编号
	 * @param operaterType 	操作类型
	 * @param busiId 		业务id
	 * @param remark 		描述
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void invokeLogInsert(String busiFunc,String operaterType,String busiId,String remark){
		try{
			IOperaterLogService operaterLogService = (IOperaterLogService)SpringContextHolder.getBean(IOperaterLogService.BEAN_ID);
			
			SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
			
			AsmgOperaterLogVo asmgOperaterLogVo = new AsmgOperaterLogVo();
			asmgOperaterLogVo.setEmpNo(sessionUser.getUserNo());
			asmgOperaterLogVo.setEmpName(sessionUser.getUserName());
			//地址得不到
			asmgOperaterLogVo.setIpaddr(sessionUser.getLoginIp());
			String busiModule = "PM";		//功能模块
			asmgOperaterLogVo.setBusiModule(busiModule);
			asmgOperaterLogVo.setBusiFunc(busiFunc);
			
			asmgOperaterLogVo.setBusiId(busiId);
			asmgOperaterLogVo.setOperaterType(operaterType);
			asmgOperaterLogVo.setLogTm(DateUtils.getCurrentDate());
			asmgOperaterLogVo.setDepict(remark);
			operaterLogService.addLog(asmgOperaterLogVo);
		}catch(Exception e){
			logger.error("记录日志期间发生异常！");
		}
	}
	
	/**
	 * 添加日志
	 * @param busiFunc 		业务功能编号
	 * @param operaterType 	操作类型
	 * @param busiId 		业务id
	 * @param remark 		描述
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void invokeLogInsert(String busiFunc,String busiId,Object originalObj, Map<String,Object> editMap){
		try{
			IOperaterLogService operaterLogService = (IOperaterLogService)SpringContextHolder.getBean(IOperaterLogService.BEAN_ID);
					
			String modifyInfo = BeanUtils.getModifyInfo(originalObj, editMap);
		
			SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
			
			AsmgOperaterLogVo asmgOperaterLogVo = new AsmgOperaterLogVo();
			asmgOperaterLogVo.setEmpNo(sessionUser.getUserNo());
			asmgOperaterLogVo.setEmpName(sessionUser.getUserName());
			//地址得不到
			asmgOperaterLogVo.setIpaddr(sessionUser.getLoginIp());
			String busiModule = "PM";		//功能模块
			asmgOperaterLogVo.setBusiModule(busiModule);
			asmgOperaterLogVo.setBusiFunc(busiFunc);
			
			asmgOperaterLogVo.setBusiId(busiId);
			asmgOperaterLogVo.setOperaterType(LogConstant.LOG_UPDATE);
			asmgOperaterLogVo.setLogTm(DateUtils.getCurrentDate());
			asmgOperaterLogVo.setDepict(modifyInfo);
			operaterLogService.addLog(asmgOperaterLogVo);
		}catch(Exception e){
			logger.error("记录日志期间发生异常！");
		}
	}
}
